package com.two.screen.water.balance.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.bean.water.ProductBean
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.DialogUtil
import com.two.screen.water.balance.utils.ToastUtil
import com.two.screen.water.balance.widget.DividerItemDecoration
import java.util.concurrent.TimeUnit


class SelectedBatchDialog(var mContext: Context) : Dialog(mContext, R.style.ChooseDialog) {

    private var mSelectedBatchDialogListener: SelectedBatchDialogListener? = null
    private var mTitleTv: TextView? = null
    private var mDialogCancelTv: TextView? = null
    private var mDialogConfirmTv: TextView? = null
    private var mCommonRecyclerView: RecyclerView? = null
    lateinit var mSelectedBatchDialogAdapter: SelectedBatchDialogAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_selected_batch)
        this.setCanceledOnTouchOutside(false)

        val window = window
        if (window != null) {
            val attributes = window.attributes
            attributes.width = App.screenWidth
            window.attributes = attributes
            window.setGravity(Gravity.CENTER)
        }

        mDialogCancelTv = findViewById(R.id.mDialogCancelTv)
        mDialogConfirmTv = findViewById(R.id.mDialogConfirmTv)
        mTitleTv = findViewById(R.id.dialog_title_tv)
        mCommonRecyclerView = findViewById(R.id.mCommonRecyclerView)

        mDialogCancelTv?.setOnClickListener { dismiss() }
        initRecyclerView()
    }

    private fun initRecyclerView() {
        (mCommonRecyclerView?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false //去闪烁
        mCommonRecyclerView?.addItemDecoration(DividerItemDecoration(mContext, DividerItemDecoration.HORIZONTAL_LIST, CommUtils.dp2px(5), ContextCompat.getColor(App.context!!, R.color.transparent)))
        mSelectedBatchDialogAdapter = SelectedBatchDialogAdapter()
        mCommonRecyclerView?.layoutManager = LinearLayoutManager(mContext)
        mCommonRecyclerView?.adapter = mSelectedBatchDialogAdapter
        mSelectedBatchDialogAdapter.bindToRecyclerView(mCommonRecyclerView)
        mSelectedBatchDialogAdapter.setEmptyView(R.layout.layout_status_layout_manager_empty1)


    }

    /*商品列表 适配器*/
    inner class SelectedBatchDialogAdapter : BaseQuickAdapter<ProductBean, BaseViewHolder>(R.layout.item_dialog_selected_batch, null) {
        @SuppressLint("CheckResult", "NotifyDataSetChanged")
        override fun convert(helper: BaseViewHolder, item: ProductBean) {
            helper
            helper.getView<TextView>(R.id.item_atv1).apply {
                text = item.supplierName?.plus("-")?.plus(String.format("%04d", item.id?.toInt() ?: 0))
                isSelected = item.isSelectedLocal
            }
            helper.getView<TextView>(R.id.item_atv2).apply {
                text = item.deliveryTime
                isSelected = item.isSelectedLocal
            }
            helper.getView<View>(R.id.item_root_view).apply {
                isSelected = item.isSelectedLocal
                setOnClickListener {
                    mData.forEach { it.isSelectedLocal = false }
                    mData[helper.bindingAdapterPosition].isSelectedLocal = true
                    notifyDataSetChanged()
                }
            }
        }
    }

    @SuppressLint("CheckResult") fun setNewData(list: MutableList<ProductBean>) {
        mSelectedBatchDialogAdapter.setNewData(list)
        mDialogConfirmTv?.let {
            RxView.clicks(it)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe {
                    var productBean = mSelectedBatchDialogAdapter.data.find { it.isSelectedLocal }
                    if (productBean == null) {
                        ToastUtil.showToast("请选择批次!")
                        return@subscribe
                    }
                    mSelectedBatchDialogListener?.onConfirm(productBean)
                }
        }
    }


    fun setSelectedBatchDialogListener(l: SelectedBatchDialogListener) {
        mSelectedBatchDialogListener = l
    }


    interface SelectedBatchDialogListener {
        fun onConfirm(item: ProductBean)
    }

    override fun show() {
        super.show()
        //隐藏状态栏
        window?.let { DialogUtil.hideNavigationBar(it) }
    }
}
